package controllers;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import services.restaurant.RestaurantService;
import beans.RestaurantVO;

@Controller
public class EjecutarAccionRestaurantController {

    @Autowired
    RestaurantService restaurantService;

    @RequestMapping(value = "public/restaurantAction", method = RequestMethod.GET)
    public ModelAndView elegirAccion(@RequestParam String submitRestaurantAction)
            throws Exception {
        ModelAndView mv = new ModelAndView();

        if (submitRestaurantAction.equals("alta")) {
            mv.setViewName("altaRestaurant");
        }
        if (submitRestaurantAction.equals("baja")) {
            mv.setViewName("bajaRestaurant");
        }
        if (submitRestaurantAction.equals("modificacion")) {
            mv.setViewName("modificacionRestaurant");
        }
        if (submitRestaurantAction.equals("seleccion")) {
            mv.setViewName("seleccionRestaurant");
        }
        return mv;
    }

    @RequestMapping(value = "public/ejecutarRestaurantAction", method = RequestMethod.GET)
    public ModelAndView ejecutarAccion(
            @ModelAttribute RestaurantVO restaurantVO,
            @RequestParam String submitRestaurantABM) throws Exception {
        ModelAndView mv = new ModelAndView();

        if (submitRestaurantABM.equals("alta")) {
            System.out.println("ENTRE A ALTA");

            this.getRestaurantService().insert(restaurantVO);

            mv.addObject("paramReasonOK", "ALTA");
            mv.setViewName("salidaOK");
        }

        if (submitRestaurantABM.equals("baja")) {
            System.out.println("ENTRE A BAJA");

            if(this.getRestaurantService().exist(restaurantVO)) {
                this.getRestaurantService().delete(restaurantVO);

                mv.addObject("paramReasonOK", "BAJA");
                mv.setViewName("salidaOK");
            } else {
                mv.addObject("paramReason", "No existe un restaurant con ese nombre para eliminar.");
                mv.setViewName("error");    
            }
        }

        if (submitRestaurantABM.equals("modificacion")) {
            System.out.println("ENTRE A MODIFICACION");

            this.getRestaurantService().update(restaurantVO);

            mv.addObject("paramReasonOK", "MODIFICACION");
            mv.setViewName("salidaOK");
        }

        if (submitRestaurantABM.equals("seleccion")) {
            System.out.println("ENTRE A SELECCION");
            List<RestaurantVO> listRestaurants = new ArrayList<RestaurantVO>();

            if (!restaurantVO.getName().equals("")) {
                listRestaurants = (List<RestaurantVO>) this
                        .getRestaurantService().selectByName(restaurantVO);

                mv.addObject("listRestaurants", listRestaurants);

                mv.setViewName("seleccionadoRestaurant");
            } else {
                listRestaurants = (List<RestaurantVO>) this
                .getRestaurantService().selectAll();

                mv.addObject("listRestaurants", listRestaurants);
            }
        }
        return mv;
    }

    public RestaurantService getRestaurantService() {
        return restaurantService;
    }

    public void setRestaurantService(RestaurantService restaurantService) {
        this.restaurantService = restaurantService;
    }
}